package test.lyj;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import test.lyj.model.CurveNetThread;
import test.lyj.model.HttpClientThread;
import test.lyj.tool.Tool;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class LogonOAActivity extends Activity {
   private EditText usernameET;
   private EditText passwordET;
    private final String PREFERENCES_NAME = "userinfo";
	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		Log.d("lyjtest", "startOnCreate");
        if (Tool.showDebugTitle) {
            requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        }
        setContentView(R.layout.logon_oa);  //这个顺序不能动
        if (Tool.showDebugTitle) {
            getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title_1);
            final TextView leftText = (TextView) findViewById(R.id.left_text);
            final TextView rightText = (TextView) findViewById(R.id.right_text);
            leftText.setText("LogonOAActivity");
        }
        Log.d("lyjtest", "startWindowSwithe");
		Button logonButton = (Button) findViewById(R.id.oa_logon);
		logonButton.setOnClickListener(logonClicker);

         usernameET = (EditText) findViewById(R.id.username);
         passwordET = (EditText) findViewById(R.id.password);
        SharedPreferences preferences = getSharedPreferences(PREFERENCES_NAME, Activity.MODE_PRIVATE);
        usernameET.setText(preferences.getString("UserName",null));
        passwordET.setText(preferences.getString("PassWord",null));

	}




    OnClickListener logonClicker = new OnClickListener() {


        public void onClick(View v) {

            excute();
        }


    };
    private void excute() {
        if(usernameET.getText().toString()==null||usernameET.getText().toString().equals("")){
            Tool.showMsgDialog(LogonOAActivity.this, "用户名不能为空");
            return ;
        }
        if(passwordET.getText().toString()==null||passwordET.getText().toString().equals("")){
            Tool.showMsgDialog(LogonOAActivity.this, "密码不能为空");
            return ;
        }
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("enterpriseName", "上海陆达"));
        params.add(new BasicNameValuePair("name", usernameET.getText().toString()));
        params.add(new BasicNameValuePair("password", passwordET.getText().toString()));
        Log.d("lyjtest", "提交的参数:name:"+usernameET.getText().toString()+"|password:"+passwordET.getText().toString()+"|enterpriseName=上海陆达");
        //线程的启动方法在NetThread 的构造函数中
        MyApplication app = (MyApplication) this.getApplication();
        new HttpClientThread(LogonOAActivity.this,messageListener,params,null,"http://co.lordor.com:888/coopera/oa/LogonOA.action",app.getHttpClient());
    }

    Handler messageListener = new Handler() {
        public void handleMessage(Message msg) {
            Bundle b = msg.getData();
            String netresult = b.getString(Tool.TIMER_TASK_RESULT);
            switch (msg.what) {

                case Tool.TIMER_TASK_TIMEOUT:
                    Tool.showMsgDialog(LogonOAActivity.this, "线程超时"+netresult);
                    break;
                case Tool.TIMER_TASK_COMPELETE:
                    if(netresult.contains("帐号不存在")||netresult.contains("帐号密码错误或帐号无效")){
                        Tool.showMsgDialog(LogonOAActivity.this, "用户名或密码错误 - -！");
                    }
                    if(netresult.contains("window.location.href=\"IndexOA.action\";")){
//                        Tool.showMsgDialog(LogonOAActivity.this, "登录成功 ^_^！");
                        Intent intent = new Intent();
                        intent.setClass(LogonOAActivity.this, SignInActivity.class);
                        startActivity(intent);
                    }
                    Log.d("lyjtest", "网络结果TIMER_TASK_COMPELETE:" + netresult);
                    break;
                case Tool.TIMER_TASK_INFO:
                    Log.d("lyjtest", "网络结果TIMER_TASK_INFO:"+netresult);
                    break;
            }
            super.handleMessage(msg);
        }
    };

    @Override
    protected void onPause() {
        super.onPause();
         Log.d("testlyj","LogonOAActivity is onpause");
        SharedPreferences agPreferences = getSharedPreferences(PREFERENCES_NAME, Activity.MODE_PRIVATE);
        SharedPreferences.Editor editor = agPreferences.edit();

       String userName = usernameET.getText().toString();
       String passWord = passwordET.getText().toString();
        editor.putString("UserName", userName);
        editor.putString("PassWord", passWord);
        editor.commit();

    }
}
